﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Runtime.Serialization.Json;
using System.IO;
using System.Text;

namespace Northwinddemo.BusinessLayer
{
    public static class JsonSerializer
    {
        public static string Serialize(object obj)
        {
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
            MemoryStream ms = new MemoryStream();
            serializer.WriteObject(ms, obj);
            return Encoding.UTF8.GetString(ms.ToArray());
        }

        public static T Deserialize<T>(string json) where T : class
        {
            MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(json));
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
            T obj = serializer.ReadObject(ms) as T;
            ms.Close();
            return obj;
        }

    }
}
